package Cam5zyx0715;



import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.security.KeyStore;
import java.util.concurrent.ForkJoinPool;

public class ImageBoard  extends  JFrame {
    //private  DrawMouse dl=null;
    String fName;
    DrawMouse mouse = new DrawMouse();

    public void initUI() {


        //窗体的创建
        this.setTitle("PC版美颜相机--by zyx");
        this.setSize(1000, 600);
        this.getContentPane().setBackground(Color.WHITE);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());

        //按钮面板  对象JPanel  默认是流式布局
        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(new Color(220, 226, 248));
        eastPanel.setPreferredSize(new Dimension(200, 0));
        this.add(eastPanel, BorderLayout.EAST);

        //图画面板
        JPanel drawPanel = new JPanel();
        drawPanel.setBackground(Color.WHITE);
        drawPanel.setPreferredSize(new Dimension(800, 500));
        this.add(drawPanel, BorderLayout.CENTER);
        mouse.j1 = drawPanel;
        drawPanel.addMouseListener(mouse);
//        drawPanel.addMouseMotionListener(mouse);


        this.setVisible(true);
        Graphics g = drawPanel.getGraphics();
        mouse.g = g;


        String[] strs = {"原图", "二值化", "灰度", "马赛克", "放大", "缩小", "油画", "哈哈镜", "旋转", "撤销"};
        Color primaryColor = new Color(137, 190, 178);
        Color textColor = Color.WHITE;
        // 应用颜色方案到按钮
        Font buttonFont = new Font("宋体", Font.PLAIN, 16); // 设置按钮字体和字号
        for (int i = 0; i < strs.length; i++) {
            JButton buDraw = new JButton(strs[i]);
            buDraw.setPreferredSize(new Dimension(80, 30));
            buDraw.setBackground(primaryColor);
            buDraw.setForeground(textColor);
            buDraw.setFont(buttonFont);
            buDraw.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            //创建一个空白边框，为按钮的内容留出一些空间，使其看起来更加整洁和美观。
            eastPanel.add(buDraw);
            buDraw.addActionListener(mouse);
        }
        JLabel j1 = new JLabel("大小/程度选择：");
        j1.setFont(new Font("黑体", 1, 15));
        eastPanel.add(j1);

        JMenuBar jmb = new JMenuBar();
        JMenu jm = new JMenu();
        jm.setText("文件");
        jmb.add(jm);
        this.setJMenuBar(jmb);
        JMenuItem open = new JMenuItem();
        open.setText("Open");
        jm.add(open);
        //按钮键->按钮功能(动作监听器)
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e != null) {
                    JFileChooser jfc = new JFileChooser();
                    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    jfc.showDialog(new Label(), "选中");
                    File file = jfc.getSelectedFile();
                    fName = file.getPath();
                    mouse.fn = fName;
                }
            }
        });

        //拉杆
        JSlider js1 = new JSlider(1, 10);
        js1.setMajorTickSpacing(1);
        js1.setMinorTickSpacing(1);
        js1.setPreferredSize(new Dimension(200, 80));
        js1.setFont(new Font("黑体", 1, 15));
        js1.setPaintLabels(true);
        js1.setPaintTicks(true);
        js1.setSnapToTicks(true);
        js1.setOpaque(false);
        js1.setValue(1);
        eastPanel.add(js1);
        js1.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                g.setColor(Color.WHITE);
                g.fillRect(0, 100, 1000, 1000);
                if (e != null) {
                    int x = js1.getValue();
                    mouse.s = x;
                }
            }
        });
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (mouse.fn != null) {
            switch (mouse.str) {
                case "原图":
                    mouse.oripic();
                    break;
                case "灰度":
                    mouse.grey();
                    break;
                case "二值化":
                    mouse.binarization();
                    break;
                case "马赛克":
                    mouse.mosaic();
                    break;
                case "放大":
                    mouse.Todouble();
                    break;
                case "缩小":
                    mouse.narrow();
                    break;
                case "油画":
                    mouse.oil();
                    break;
                case "哈哈镜":
                    mouse.funhouseMirror();
                    break;
                case"旋转":
                    mouse.spin();
                    break;
                case "撤销":
                    mouse.undoImage();
                    break;
                default:
                    break;
            }
        }
    }

    public static void main(String[] args) {
        new ImageBoard().initUI();
    }

}
